package com.edu.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.bean.Admin;
import com.edu.service.AdminService;
import com.edu.service.impl.AdminServiceImpl;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		HttpSession session = req.getSession();
		AdminService as = new AdminServiceImpl();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		Cookie cookie = new Cookie("uname",username);
		Cookie cookie2 = new Cookie("upwd",password);
		cookie.setMaxAge(60*60*24*7);
		cookie2.setMaxAge(60*60*24*7);
		resp.addCookie(cookie);
		resp.addCookie(cookie2);
		Map<String,Object> map =as.selectAll(username, password);
		if(map.get("loginAdmin")!=null){
			session.setAttribute("loginAdmin", map.get("loginAdmin"));
			resp.sendRedirect("admin.jsp");
		}else{
			req.setAttribute("msg",map.get("msg"));
			req.getRequestDispatcher("login.html").forward(req,resp);
		}
		
	}
}
